package com.wms.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wms.common.QueryPageParam;
import com.wms.common.Result;
import com.wms.entity.Billing;
import com.wms.entity.Goods;
import com.wms.service.BillingService;
import com.wms.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author yiyun
 * @since 2024-6-15
 */
@RestController
@RequestMapping("/billing")
public class BillingController {

   @Autowired
    private BillingService billingService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Billing billing){
        return billingService.save(billing)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Billing billing){
        return billingService.updateById(billing)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return billingService.removeById(id)?Result.suc():Result.fail();
    }

    // 查询
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        // 获取前端数据
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String price = (String)param.get("price");
        String createTime = (String)param.get("createTime");

        // 分页
        Page<Billing> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        // mybatis查询
        LambdaQueryWrapper<Billing> lambdaQueryWrapper = new LambdaQueryWrapper();

        // 判空
        if(StringUtils.isNotBlank(name) && !"null".equals(name)){
            lambdaQueryWrapper.like(Billing::getName,name);
        }
        if(StringUtils.isNotBlank(price) && !"null".equals(price)){
            lambdaQueryWrapper.eq(Billing::getPrice,price);
        }
        if(StringUtils.isNotBlank(createTime) && !"null".equals(createTime)){
            lambdaQueryWrapper.eq(Billing::getCreateTime,createTime);
        }

        // 获取结果
        IPage result = billingService.pageCC(page,lambdaQueryWrapper);

        // 返回前端
        return Result.suc(result.getRecords(),result.getTotal());
    }


    public static void main(String[] args) {
        System.out.println("第一次写java");
        System.out.println("第二次写java");
    }
}
